package com.glasgowrax.hack.task;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

import com.glasgowrax.hack.domain.Feature;
import com.glasgowrax.hack.interfaces.DownloadsWalkingDirections;

public class DownloadWalkingDirectionsTask extends AsyncTask<String, String, String> {
	
	private static final String DIRECTIONS_API_URL = "http://maps.googleapis.com/maps/api/directions/json?";
	private static final String ORIGIN = "origin";
	private static final String DESTINATION = "destination";
	private static final String SENSOR = "sensor";
	private static final String ALTERNATIVES = "alternatives";
	private static final String DEPARTURE_TIME = "departure_time";
	private static final String ARRIVAL_TIME = "arrival_time";
	private static final String MODE = "mode";
	
	private static final String SENSOR_DEFAULT = "false";
	private static final String MODE_DEFAULT = "transit";

	private DownloadsWalkingDirections display;
	private String origin;
	private String destination;
	private boolean alternatives;
	private boolean cycle;	
	private Feature rack;
	
	private Long departureTime;
	private Long arrivalTime;
	
	public DownloadWalkingDirectionsTask(DownloadsWalkingDirections display, String origin, String destination, boolean alternatives, boolean cycle, Feature rack) {
		super();
		this.display = display;
		this.origin = origin;
		this.destination = destination;
		this.alternatives = alternatives;
		this.departureTime = departureTime;
		this.arrivalTime = arrivalTime;
		this.rack = rack;
	}
	
	/** Download images from urls. */
	@Override
	protected String doInBackground(String... urls) {
		
		String result = "";
//		String urlString = urls[0];
		
		if (this.isCancelled()) return null;
		
		try {

			// Determine if time parameter is departure or arrival
			String timeKey, timeValue;
			if (arrivalTime != null) {
				timeKey = ARRIVAL_TIME;
				timeValue = "" + arrivalTime;
			} else if (departureTime != null) {
				timeKey = DEPARTURE_TIME;
				timeValue = "" + departureTime;
			} else {
				timeKey = DEPARTURE_TIME;
				timeValue = "" + System.currentTimeMillis();
			}
			
			// Set URL parameters
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put(ORIGIN, origin);
			parameters.put(DESTINATION, destination);
			parameters.put(SENSOR, SENSOR_DEFAULT);
			parameters.put(ALTERNATIVES, (alternatives) ? "true" : "false");
			parameters.put(timeKey, timeValue);
			parameters.put(ARRIVAL_TIME, origin);
			parameters.put(MODE, MODE_DEFAULT);
			
			// Create URL and request
			//URI uri = new URI(DIRECTIONS_API_URL + new QueryString(parameters).toString());
			URI uri = new URI("http://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&sensor=false"+"&mode=walking");
			Log.i("DIRECTIONS", uri.toString());
			DefaultHttpClient httpClient = new DefaultHttpClient();
		    HttpGet httpGet = new HttpGet(uri);

		    // Get response
		    HttpResponse httpResponse = httpClient.execute(httpGet);
		    HttpEntity httpEntity = httpResponse.getEntity();
		    String response = EntityUtils.toString(httpEntity);
			publishProgress(response);	
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/** Update UI when each image downloads. */
	@Override
	protected void onProgressUpdate(String... result) {	
		
		display.updateDisplayWithWalking(rack, result[0]);
		display.onWalkingDirectionsDownloadFinished();
    }
	
}